package com.wsz.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;;
import com.wsz.eduservice.pojo.Course;
import com.wsz.eduservice.pojo.CourseDescription;
import com.wsz.eduservice.pojo.Teacher;
import com.wsz.eduservice.pojo.frontvo.CourseFrontVo;
import com.wsz.eduservice.pojo.frontvo.CourseWebVo;
import com.wsz.eduservice.pojo.vo.CourseInfoVo;
import com.wsz.eduservice.pojo.vo.CoursePublic;
import com.wsz.eduservice.pojo.vo.PublishCourseQuery;
import com.wsz.eduservice.service.ChapterService;
import com.wsz.eduservice.service.CourseDescriptionService;
import com.wsz.eduservice.service.CourseService;
import com.wsz.eduservice.mapper.CourseMapper;
import com.wsz.eduservice.service.VideoService;
import com.wsz.servicebase.exceptionhandler.MyTestException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author wangs
* @description 针对表【edu_course(课程)】的数据库操作Service实现
* @createDate 2022-09-24 19:04:35
*/
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course>
    implements CourseService{

    @Autowired
    private CourseDescriptionService courseDescriptionService;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private VideoService videoService;

    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoVo,course);
        int insert = baseMapper.insert(course);

        if(insert == 0){
            new MyTestException(20001,"保存课程失败");
        }

        CourseDescription courseDescription = new CourseDescription();
        BeanUtils.copyProperties(courseInfoVo,courseDescription);
        courseDescription.setId(course.getId());
        boolean save = courseDescriptionService.save(courseDescription);
        if(save == false){
            new MyTestException(20001,"课程简介添加失败");
        }

        return course.getId();
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        CourseInfoVo courseInfoVo = new CourseInfoVo();

        Course course = baseMapper.selectById(courseId);
        BeanUtils.copyProperties(course,courseInfoVo);

        CourseDescription description = courseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(description.getDescription());

        return courseInfoVo;
    }

    @Override
    public void updaCouresInfo(CourseInfoVo courseInfoVo) {
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoVo,course);
        int i = baseMapper.updateById(course);
        if(i == 0){
            new MyTestException(20001,"修改课程失败");
        }

        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescription.setId(courseInfoVo.getId());
        courseDescriptionService.updateById(courseDescription);


    }

    @Override
    public CoursePublic PublishCourseInfo(String courseId) {
        CoursePublic publishCourseInfo = baseMapper.getPublishCourseInfo(courseId);
        return publishCourseInfo;
    }

    @Override
    public Page<CoursePublic> getCoursePublicPage(Page<CoursePublic> pagey,
                                                 String title,
                                                 String teacherName,
                                                 Integer lessonNum,
                                                 String subjectLevelOne,
                                                 String subjectLevelTwo) {
        Page<CoursePublic> publishCoursePage = baseMapper.getCoursePublicPage(pagey, title, teacherName,lessonNum,
                subjectLevelOne,subjectLevelTwo);
        return publishCoursePage;
    }

    @Override
    public List<CoursePublic> getCoursePublicAll() {
        List<CoursePublic> list = baseMapper.getCoursePublicAll();
        return list;
    }

    @Override
    public void removeByCourseId(String courseId) {
        videoService.removeByCourseId(courseId);

        chapterService.removeByCourseId(courseId);

        courseDescriptionService.removeByCourseId(courseId);

        int delete = baseMapper.deleteById(courseId);
        if (delete <= 0) new MyTestException(20001, "删除失败");
    }

    @Cacheable(key = "'selectCourse'",value = "Course")
    @Override
    public List<Course> getCourseIndex() {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.last("limit 8");

        return this.list(queryWrapper);
    }

    @Override
    public Map<String,Object> getCourseFrontList(Page<Course> pages, CourseFrontVo courseFrontVo) {

        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        String one = courseFrontVo.getSubjectParentId();
        String two = courseFrontVo.getSubjectId();
        String buy = courseFrontVo.getBuyCountSort();
        String price = courseFrontVo.getPriceSort();
        String gmt = courseFrontVo.getGmtCreateSort();
        if(StringUtils.isNotEmpty(one)){
            queryWrapper.eq("subject_parent_id",one);
        }
        if(StringUtils.isNotEmpty(two)){
            queryWrapper.eq("subject_id",two);
        }
        if(StringUtils.isNotEmpty(buy)){
            queryWrapper.orderByDesc("buy_count");
        }
        if(StringUtils.isNotEmpty(gmt)){
            queryWrapper.orderByDesc("gmt_create");
        }
        if(StringUtils.isNotEmpty(price)){
            queryWrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pages,queryWrapper);

        List<Course> records = pages.getRecords();
        long current = pages.getCurrent();
        long pagess = pages.getPages();
        long size = pages.getSize();
        long total = pages.getTotal();
        boolean hasNext = pages.hasNext();
        boolean hasPrevious = pages.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pagess);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    @Override
    public CourseWebVo getBaseCourseId(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }

    @Override
    public List<Course> getBuyCourse(List<String> list) {
        List<Course> courses = baseMapper.selectBatchIds(list);
        return courses;
    }
}




